//
//  UIOC.m
//  jiasicong
//
//  Created by wandong on 2018/8/16.
//  Copyright © 2018年 jiasicong. All rights reserved.
//

#import "UIOC.h"
#import "MBProgressHUD.h"
//透明显示条
#define HudBarInstance [HudBar shareInstance]
@interface HudBar()

@property (nonatomic, strong) MBProgressHUD *HUD;

@end
@implementation HudBar

+(instancetype)shareInstance{
    static HudBar *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HudBar alloc]init];
    });
    return instance;
}

+(void)setHudProgressObject:(NSProgress*)p{
    dispatch_async(dispatch_get_main_queue(), ^{
        HudBarInstance.HUD.progressObject = p;
    });
}

+(void)showMsg:(NSString*)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.detailsLabel.font = [UIFont systemFontOfSize:16];
        hud.userInteractionEnabled = NO;
        hud.mode = MBProgressHUDModeText;
        if (str) {
            hud.detailsLabel.text = str;
        }else{
            hud.detailsLabel.text = @"网络链接失败";
        }
        //        hud.margin = 10.0;
        //        CGPoint tmp = hud.offset;
        //        tmp.y = 64.0;
        //        hud.offset = tmp;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2];
    });
}

+(void)showPercent:(NSString*)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        HudBarInstance.HUD = [MBProgressHUD showHUDAddedTo:window animated:YES];
        [window addSubview:HudBarInstance.HUD];
        HudBarInstance.HUD.mode = MBProgressHUDModeAnnularDeterminate;
        HudBarInstance.HUD.removeFromSuperViewOnHide = YES;
        HudBarInstance.HUD.detailsLabel.font = [UIFont systemFontOfSize:16];
        HudBarInstance.HUD.detailsLabel.text = str?str:@"处理中...";
    });
}

+(void)showSuccess:(NSString *)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        HudBarInstance.HUD = [MBProgressHUD showHUDAddedTo:window animated:YES];
        [window addSubview:HudBarInstance.HUD];
        HudBarInstance.HUD.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"appicon"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        HudBarInstance.HUD.customView = [[UIImageView alloc] initWithImage:image];
        HudBarInstance.HUD.square = YES;
        
        HudBarInstance.HUD.detailsLabel.font = [UIFont systemFontOfSize:16];
        HudBarInstance.HUD.detailsLabel.text = str?str:@"提交成功!";
        HudBarInstance.HUD.removeFromSuperViewOnHide = YES;
        [HudBarInstance.HUD hideAnimated:YES afterDelay:2];
    });
}

+(void)waitingMsg{
    dispatch_async(dispatch_get_main_queue(), ^{
        //    [HudBar showCMLoadingView];
        [HudBar waitingMsg:@"请稍等..."];
    });
}

+(void)waitingMsg:(NSString*)str{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    HudBarInstance.HUD = [MBProgressHUD showHUDAddedTo:window animated:YES];
    [window addSubview:HudBarInstance.HUD];
    //    CGPoint tmp = HudBarInstance.HUD.offset;
    //    tmp.y = 64.0;
    //    HudBarInstance.HUD.offset = tmp;
    HudBarInstance.HUD.mode = MBProgressHUDModeIndeterminate;
    HudBarInstance.HUD.removeFromSuperViewOnHide = YES;
    if (str) {
        HudBarInstance.HUD.detailsLabel.font = [UIFont systemFontOfSize:16];
        HudBarInstance.HUD.detailsLabel.text = str;
    }
    [HudBarInstance.HUD hideAnimated:YES afterDelay:10];
}
+(void)hideWaiting{
    dispatch_async(dispatch_get_main_queue(), ^{
        [HudBarInstance.HUD hideAnimated:YES];
        HudBarInstance.HUD = nil;
    });
}
/*
 +(void)showCMLoadingView{
 UIWindow *window = [[UIApplication sharedApplication] keyWindow];
 HudBarInstance.HUD = [MBProgressHUD showHUDAddedTo:window animated:YES];
 [window addSubview:HudBarInstance.HUD];
 //    HudBarInstance.HUD.offset = CGPointMake(0, 0);
 //    HudBarInstance.HUD.margin = 0;
 HudBarInstance.HUD.mode = MBProgressHUDModeCustomView;
 HudBarInstance.HUD.removeFromSuperViewOnHide = YES;
 CMLoadingView *view = [[CMLoadingView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
 //    view.bg.alpha = 1;
 //    view.bg.backgroundColor = [UIColor whiteColor];
 
 HudBarInstance.HUD.customView = view;
 view.bounds = CGRectMake(40, 40, 80, 80);
 //            view.center = CGPointMake(40, 40);
 HudBarInstance.HUD.backgroundColor = [UIColor clearColor];
 
 [HudBarInstance.HUD hideAnimated:YES afterDelay:10];
 }
 */
@end

//color
@implementation UIColor(productone)
+(UIColor *) hexStringToColor: (int) hex{
    return UIColorFromRGB(hex);
}
+ (UIColor*)pinkThemeColor{
    return UIColorFromRGB(0xff0080);
}
+ (UIColor*)violetThemeColor{
    return UIColorFromRGB(0x9977f4);
}
+ (UIColor*)loginSeperatorColor{
    return UIColorFromRGB(0xe5e5e5);
}
+ (UIColor*)cellBlack3TextColor{
    return UIColorFromRGB(0x333333);
}
+ (UIColor*)cellBlack6TextColor{
    return UIColorFromRGB(0x666666);
}
+ (UIColor*)cellBlack9TextColor{
    return UIColorFromRGB(0x999999);
}
+ (UIColor*)textFieldPlaceholderColor{
    return UIColorFromRGB(0x99999);
}
+ (UIColor*)redThemeColor{
    return UIColorFromRGB(0x111);
}

+ (UIColor*)greenThemeColor{
    return UIColorFromRGB(0x1111);
}
+ (UIColor*)blueThemeColor{
    return UIColorFromRGB(0x111);
}

//浅蓝
+ (UIColor*)lightBlueThemeColor{
    return UIColorFromRGB(0x000111);
}
+ (UIColor*)grayThemeColor{
    //    DLog(@"%d",LazyColor(@"grayThemeColor"))
    return UIColorFromRGB(0x111);
}

//tableview color
+ (UIColor*)tableSeparatorColor{
    return UIColorFromRGB(0xfcf3f8);
}

+ (UIColor*)tableBGColor{
    return UIColorFromRGB(0xfcf3f8);
//    return UIColorFromRGB(0xf6f6f6);
}

//login color
+ (UIColor*)loginBGColor{
    return UIColorFromRGB(0x82112);
}

+ (UIColor*)loginTextFieldColor{
    return UIColorFromRGB(0x22121);
}

+ (UIColor*)loginTextColor{
    return UIColorFromRGB(0x9328);
}
@end

@implementation UIView (PYExtension)

- (void)setPy_x:(CGFloat)py_x
{
    CGRect frame = self.frame;
    frame.origin.x = py_x;
    self.frame = frame;
}

- (CGFloat)py_x
{
    return self.py_origin.x;
}

- (void)setPy_centerX:(CGFloat)py_centerX
{
    CGPoint center = self.center;
    center.x = py_centerX;
    self.center = center;
}

- (CGFloat)py_centerX
{
    return self.center.x;
}

-(void)setPy_centerY:(CGFloat)py_centerY
{
    CGPoint center = self.center;
    center.y = py_centerY;
    self.center = center;
}

- (CGFloat)py_centerY
{
    return self.center.y;
}

- (void)setPy_y:(CGFloat)py_y
{
    CGRect frame = self.frame;
    frame.origin.y = py_y;
    self.frame = frame;
}

- (CGFloat)py_y
{
    return self.frame.origin.y;
}

- (void)setPy_size:(CGSize)py_size
{
    CGRect frame = self.frame;
    frame.size = py_size;
    self.frame = frame;
    
}

- (CGSize)py_size
{
    return self.frame.size;
}

- (void)setPy_height:(CGFloat)py_height
{
    CGRect frame = self.frame;
    frame.size.height = py_height;
    self.frame = frame;
}

- (CGFloat)py_height
{
    return self.frame.size.height;
}

- (void)setPy_width:(CGFloat)py_width
{
    CGRect frame = self.frame;
    frame.size.width = py_width;
    self.frame = frame;
    
}
- (CGFloat)py_width
{
    return self.frame.size.width;
}

- (void)setPy_origin:(CGPoint)py_origin
{
    CGRect frame = self.frame;
    frame.origin = py_origin;
    self.frame = frame;
}

- (CGPoint)py_origin
{
    return self.frame.origin;
}

@end
@implementation UITableView(catagray)

- (UIView*)getLabelHeader:(NSString *)t{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor grayColor];
    label.text = [NSString stringWithFormat:@"    %@",t];
    return label;
}

- (void)initTableOffset{
    if (self.style == UITableViewStyleGrouped) {
        self.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    }
}

- (void)initTableOffset1{
    if (self.style == UITableViewStyleGrouped) {
        self.contentInset = UIEdgeInsetsMake(-24, 0, 0, 0);
    }
}

- (void)initTableColor{
    //    self.separatorColor = [UIColor tableSeparatorColor];
    //    self.backgroundColor = [UIColor tableBGColor];
}

@end

//imageView
@implementation UIImageView(placeholder_text)
- (NSString *)getNameText:(NSString*) text{
    NSMutableString * firstCharacters = [NSMutableString string];
    NSArray * words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    for (NSString * word in words) {
        if ([word length] > 0) {
            NSString * firstLetter = [word substringWithRange:[word rangeOfComposedCharacterSequenceAtIndex:0]];
            [firstCharacters appendString:[firstLetter uppercaseString]];
        }
    }
    return firstCharacters;
}

- (UIImage*)getTextImg:(NSString*)text{
    CGRect frame = self.frame;
    
    // General Declarations
    UIGraphicsBeginImageContextWithOptions(frame.size, YES, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Color Declarations
    UIColor* backgroundColor = [UIColor lightGrayColor];
    
    // Variable Declarations
    NSString* initials = text;
    CGFloat fontSize = frame.size.height / 2.8;
    
    // Rectangle Drawing
    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), CGRectGetWidth(frame), CGRectGetHeight(frame))];
    [backgroundColor setFill];
    [rectanglePath fill];
    
    // Initials String Drawing
    CGRect initialsStringRect = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), CGRectGetWidth(frame), CGRectGetHeight(frame));
    NSMutableParagraphStyle* initialsStringStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
    initialsStringStyle.alignment = NSTextAlignmentCenter;
    
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    
    NSDictionary* initialsStringFontAttributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor whiteColor], NSParagraphStyleAttributeName: initialsStringStyle};
    
    CGFloat initialsStringTextHeight = [initials boundingRectWithSize: CGSizeMake(initialsStringRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: initialsStringFontAttributes context: nil].size.height;
    CGContextSaveGState(context);
    CGContextClipToRect(context, initialsStringRect);
    [initials drawInRect: CGRectMake(CGRectGetMinX(initialsStringRect), CGRectGetMinY(initialsStringRect) + (CGRectGetHeight(initialsStringRect) - initialsStringTextHeight) / 2, CGRectGetWidth(initialsStringRect), initialsStringTextHeight) withAttributes: initialsStringFontAttributes];
    CGContextRestoreGState(context);
    
    return UIGraphicsGetImageFromCurrentImageContext();
}

- (UIImage*)getNameTextImg:(NSString*)text{
    return [self getTextImg:[self getNameText:text]];
}

@end

@implementation UIImageViewWithMenu
-(instancetype)init{
    if (self = [super init]) {
        [self commonInit];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
}

- (void)setMenuTitles:(NSArray *)menuTitles model:(id)model block:(SActionBlock1)block{
    if (menuTitles.count > 5) {
        DLog(@"菜单太多，暂时支持5个");
        return;
    }
    _menuTitles = menuTitles;
    _model = model;
    _block = block;
}

- (void)show{
    if (!_menuTitles) {
        DLog(@"没有初始化 menuTitles");
        return;
    }
    [self becomeFirstResponder];
    UIMenuController * menu = [UIMenuController sharedMenuController];
    if (menu.isMenuVisible) {
        return;
    }else{
        _menuItems = [NSMutableArray new];
        for (int i=0; i<_menuTitles.count; i++) {
            NSString *actionTitle = [NSString stringWithFormat:@"menuAction%d:",i];
            [_menuItems addObject:[[UIMenuItem alloc]initWithTitle:_menuTitles[i] action:NSSelectorFromString(actionTitle)]];
        }
        menu.menuItems = _menuItems;
        [menu setTargetRect:self.bounds inView:self];
        [menu setMenuVisible:YES animated:YES];
    }
}

- (void)tap:(UIGestureRecognizer*)sender{
    if (sender.state == UIGestureRecognizerStateEnded) {
        [self show];
    }
}

- (void)menuAction0:(UIMenuController*)sender{
    if (self.block) {
        self.block((_model?@[@0,_model]:@[@0]));
    }
}

- (void)menuAction1:(UIMenuController*)sender{
    if (self.block) {
        self.block((_model?@[@1,_model]:@[@1]));
    }
}

- (void)menuAction2:(UIMenuController*)sender{
    if (self.block) {
        self.block((_model?@[@2,_model]:@[@2]));
    }
}

- (void)menuAction3:(UIMenuController*)sender{
    if (self.block) {
        self.block((_model?@[@3,_model]:@[@3]));
    }
}

- (void)menuAction4:(UIMenuController*)sender{
    if (self.block) {
        self.block((_model?@[@4,_model]:@[@4]));
    }
}

- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    for (UIMenuItem *item in _menuItems) {
        if (action == item.action) {
            return YES;
        }
    }
    return NO;
}
@end

@implementation SCollectionViewLayout

@end
